set matsize 11000
clear
set more off 

use "Data_complete.dta"
encode employee_id, generate(employee)
encode employer_id, generate(firm)

drop if not_found==1
*employee A0101402Y2 is working for two firms and changing treatment, so he will be dropped
drop if employee_id=="A0101402Y2"

label variable bowls_peeled "Bowls peeled"
label variable goals "Goals"
label variable Post "Post"

winsor bowls_peeled, generate(bowls_peeled_w)  p(0.05)
winsor peeling_time, generate(peeling_time_w)  p(0.05)
generate productivity=bowls_peeled/peeling_time_w
winsor productivity, generate(productivity_w) p(0.05)

generate bowls_pre=bowls_peeled_w if Post==0
generate peeling_pre=peeling_time_w if Post==0
generate productivity_pre=productivity_w if Post==0

sort firm date_peeled
by firm date_peeled: egen n_workers=count(employee)
by firm date_peeled: egen n_bowls=sum(bowls_peeled)
by firm date_peeled: generate tfp=n_bowls/n_workers
winsor tfp, generate(tfp_w) p(0.05)

sort employer_id date_peeled
generate tfp_pre=tfp if Post==0

sort employee Post 
by employee Post: gen obs=_n

drop if Post==1
egen merge_id=concat(employee obs)
keep employee bowls_pre peeling_pre productivity_pre tfp_pre obs merge_id firm goals piecerate
save "bowls_pre.dta", replace

clear 
use "Data_complete.dta"
encode employee_id, generate(employee)
encode employer_id, generate(firm)
by employee, sort: gen n_employee = _n == 1

drop if not_found==1
*employee A0101402Y2 is working for two firms and changing treatment, so he will be dropped
drop if employee_id=="A0101402Y2"

label variable bowls_peeled "Bowls peeled"
label variable goals "Goals"
label variable Post "Post"

winsor bowls_peeled, generate(bowls_peeled_w)  p(0.05)
winsor peeling_time, generate(peeling_time_w)  p(0.05)
generate productivity=bowls_peeled/peeling_time_w
winsor productivity, generate(productivity_w) p(0.05)

gsort employee date_peeled Post 
generate bowls_post=bowls_peeled_w if Post==1
generate peeling_post=peeling_time_w if Post==1
generate productivity_post=productivity_w if Post==1

sort firm date_peeled
by firm date_peeled: egen n_workers=count(employee)
by firm date_peeled: egen n_bowls=sum(bowls_peeled)
by firm date_peeled: generate tfp=n_bowls/n_workers
generate tfp_post=tfp if Post==1

sort employee Post 
by employee Post: gen obs=_n

drop if Post==0
keep employee date_peeled bowls_post peeling_post productivity_post tfp_post obs week firm goals piecerate

egen merge_id=concat(employee obs)

save "bowls_post.dta", replace

use "bowls_pre.dta"

merge 1:1 merge_id using "bowls_post"

save "ancova.dta", replace

preserve
set more off
collapse (mean) bowls_pre bowls_post peeling_pre peeling_post productivity_pre productivity_post tfp_pre tfp_post piecerate, by(date_peeled employee firm goals week) 
sort employee
by employee: egen mean_pre=mean(bowls_pre)
by employee: egen mean_pre_time=mean(peeling_pre)
by employee: egen mean_pre_prod=mean(productivity_pre)
sort firm
by firm: egen mean_pre_labor=mean(tfp_pre)
reg bowls_post mean_pre goals i.week, cluster(firm)
*outreg2 using "TableAncova", tex replace addtext(Clustered S.E, Firm) label  
reg peeling_post mean_pre_time goals i.week, cluster(firm)
*outreg2 using "TableAncova", tex append addtext(Clustered S.E, Firm) label
reg productivity_post mean_pre_prod goals i.week, cluster(firm)
*outreg2 using "TableAncova", tex append addtext(Clustered S.E, Firm) label
reg tfp_post mean_pre_labor goals i.week, cluster(firm)
*outreg2 using "TableAncova", tex append addtext(Clustered S.E, Firm) label
restore

